package com.eshop.modules.customer.rest;

import java.lang.invoke.SerializedLambda;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.eshop.modules.monitor.domain.Visits;
import org.springframework.web.bind.annotation.DeleteMapping;
import java.io.Serializable;
import java.util.Arrays;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.PostMapping;
import com.eshop.modules.aop.ForbidSubmit;
import com.eshop.exception.BadRequestException;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.validation.annotation.Validated;
import com.eshop.modules.customer.domain.StoreCustomer;
import org.springframework.http.HttpStatus;
import com.eshop.domain.PageResult;
import org.springframework.http.ResponseEntity;
import org.springframework.data.domain.Pageable;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import io.swagger.annotations.ApiOperation;
import com.eshop.logging.aop.log.Log;
import java.io.IOException;
import com.eshop.modules.customer.service.dto.StoreCustomerDto;
import com.eshop.modules.customer.service.dto.StoreCustomerQueryCriteria;
import javax.servlet.http.HttpServletResponse;
import com.eshop.dozer.service.IGenerator;
import com.eshop.modules.customer.service.StoreCustomerService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;

@Api(tags = { "customer管理" })
@RestController
@RequestMapping({ "/api/yxStoreCustomer" })
public class YxStoreCustomerController
{
    private final StoreCustomerService storeCustomerService;
    private final IGenerator generator;
    
    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping({ "/download" })
    @PreAuthorize("@el.check('admin','yxStoreCustomer:list')")
    public void download(final HttpServletResponse response, final StoreCustomerQueryCriteria criteria) throws IOException {
        this.storeCustomerService.download(this.generator.convert(this.storeCustomerService.queryAll(criteria), (Class)StoreCustomerDto.class), response);
    }
    
    @GetMapping
    @Log("查询customer")
    @ApiOperation("查询customer")
    @PreAuthorize("@el.check('admin','yxStoreCustomer:list')")
    public ResponseEntity<PageResult<StoreCustomerDto>> getYxStoreCustomers(final StoreCustomerQueryCriteria criteria, final Pageable pageable) {
        return (ResponseEntity<PageResult<StoreCustomerDto>>)new ResponseEntity(this.storeCustomerService.queryAll(criteria, pageable), HttpStatus.OK);
    }
    
    @ForbidSubmit
    @PostMapping
    @Log("新增customer")
    @ApiOperation("新增customer")
    @PreAuthorize("@el.check('admin','yxStoreCustomer:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody final StoreCustomer resources) {
        final int count = this.storeCustomerService.count(Wrappers.<StoreCustomer>lambdaQuery().eq(StoreCustomer::getOpenId, resources.getOpenId()));
        if (count > 0) {
            throw new BadRequestException("当前用户已存在，请勿重复提交");
        }
        return new ResponseEntity(this.storeCustomerService.save(resources), HttpStatus.CREATED);
    }
    
    @ForbidSubmit
    @PutMapping
    @Log("修改customer")
    @ApiOperation("修改customer")
    @PreAuthorize("@el.check('admin','yxStoreCustomer:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody final StoreCustomer resources) {
        this.storeCustomerService.updateById(resources);
        return new ResponseEntity(HttpStatus.NO_CONTENT);
    }
    
    @ForbidSubmit
    @Log("删除customer")
    @ApiOperation("删除customer")
    @PreAuthorize("@el.check('admin','yxStoreCustomer:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody final Long[] ids) {
        Arrays.asList(ids).forEach(id -> this.storeCustomerService.removeById((Serializable)id));
        return new ResponseEntity(HttpStatus.OK);
    }
    
    public YxStoreCustomerController(final StoreCustomerService storeCustomerService, final IGenerator generator) {
        this.storeCustomerService = storeCustomerService;
        this.generator = generator;
    }
}
